package financial.sharing.center.invoice.service.impl;

import com.github.pagehelper.PageInfo;
import financial.sharing.center.common.enums.*;
import financial.sharing.center.common.utils.BigDecimalUtil;
import financial.sharing.center.common.utils.StringUtil;
import financial.sharing.center.invoice.dao.InvoiceMapper;
import financial.sharing.center.invoice.feign.SystemFeignTestService;
import financial.sharing.center.invoice.model.entity.Invoice;
import financial.sharing.center.invoice.model.entity.InvoiceDetail;
import financial.sharing.center.invoice.service.InvoiceDetailService;
import financial.sharing.center.invoice.service.InvoiceService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName InvoiceServiceImpl
 * @Description TODO
 * @Author guoyuhang
 * @ @csdn https://blog.csdn.net/qq_40788718
 * @Date 2021/5/13
 * @Version 1.0
 */
@Service
public class InvoiceServiceImpl implements InvoiceService {

    @Autowired
    private InvoiceMapper invoiceMapper;

    @Autowired
    private InvoiceDetailService invoiceDetailServiceImpl;

    @Autowired
    private SystemFeignTestService systemFeignTestService;

    @Override
    public PageInfo<Invoice> getAllInvoice(String selectInvoiceContext, Integer selectInvoiceTypeValue, Integer selectInvoiceColorValue) {
        Example example = new Example(Invoice.class);
        Example.Criteria criteria = example.createCriteria().andEqualTo("enableStatusValue", EnableStatusEnum.正常.getValue());
        if (!StringUtil.isEmpty(selectInvoiceContext)){
            criteria.andLike("invoiceContext", StringUtil.toLike(selectInvoiceContext));
        }
        if (!InvoiceColorEnum.无.getCode().equals(selectInvoiceColorValue)){
            criteria.andEqualTo("invoiceColorValue",selectInvoiceColorValue);
        }
        if (!InvoiceTypeEnum.无.getValue().equals(selectInvoiceTypeValue)){
            criteria.andEqualTo("invoiceTypeValue", selectInvoiceTypeValue);
        }
        Map<Integer, BigDecimal> taxRateMap = this.systemFeignTestService.getTaxRateMap();
        List<Invoice> invoiceList = this.invoiceMapper.selectByExample(example);
        for (Invoice invoice : invoiceList) {
            invoice.setInvoiceDetail(this.invoiceDetailServiceImpl.getInvoiceDetail(invoice));
            invoice.setInvoiceColorName(InvoiceColorEnum.int2Enum(invoice.getInvoiceColorValue()).getName());
            invoice.setInvoiceTypeName(InvoiceTypeEnum.int2Enum(invoice.getInvoiceTypeValue()).getName());
            invoice.setInvoiceOperationStageName(InvoiceOperationStageEnum.int2Enum(invoice.getInvoiceOperationStageValue()).getName());
            if (invoice.getInvoiceDetail().getTaxRateId() != null){
                BigDecimal taxRate = taxRateMap.get(invoice.getInvoiceDetail().getTaxRateId());
                invoice.getInvoiceDetail().setTaxMoney(invoice.getInvoiceDetail().getTotalMoney().multiply(taxRate));
                invoice.getInvoiceDetail().setTotalTaxMoney(invoice.getInvoiceDetail().getTotalMoney().add(invoice.getInvoiceDetail().getTotalMoney()));
            }
        }
        return new PageInfo<>(invoiceList);
    }

    @Override
    public Integer createOneInvoice(String invoiceContext, Integer invoiceTypeValue, Integer invoiceColorValue) {
        Invoice invoice = new Invoice(invoiceContext, invoiceTypeValue, invoiceColorValue);
        this.invoiceMapper.insertSelective(invoice);
        InvoiceDetail invoiceDetail = new InvoiceDetail(invoice.getId(),invoiceContext);
        return this.invoiceDetailServiceImpl.createOneInvoiceDetail(invoiceDetail);
    }

    @Override
    public Integer compileOneInvoice(Invoice invoice) {
        int i = this.invoiceMapper.updateByPrimaryKey(invoice);
        int j = this.invoiceDetailServiceImpl.compileOneInvoiceDetail(invoice.getInvoiceDetail());
        return i == 1 && j == ResponseContext.成功.getValue() ? ResponseContext.成功.getValue() : ResponseContext.更新失败.getValue();
    }

    @Override
    public Integer deleteOneInvoice(Integer id) {
        Invoice invoice = this.getInvoice(id);
        invoice.setEnableStatusValue(EnableStatusEnum.弃用.getValue());
        int i = this.invoiceMapper.updateByPrimaryKey(invoice);
        return i == 1 ? ResponseContext.成功.getValue() : ResponseContext.删除失败.getValue();
    }

    @Override
    public Invoice getInvoice(Integer id) {
        return this.invoiceMapper.selectByPrimaryKey(id);
    }

    @Override
    public Map<Integer, BigDecimal> getMapInvoice() {
        Example example = new Example(Invoice.class);
        example.createCriteria().andEqualTo("enableStatusValue", EnableStatusEnum.正常.getValue());
        List<Invoice> invoiceList = this.invoiceMapper.selectByExample(example);
        Map<Integer,BigDecimal> invoiceDetailMap = this.invoiceDetailServiceImpl.getMapInvoiceDetail();
        Map<Integer, BigDecimal> result = new HashMap<>();
        for (Invoice invoice : invoiceList) {
            if (InvoiceOperationStageEnum.已作废.getValue().equals(invoice.getInvoiceOperationStageValue())){
                continue;
            }
            if (invoice.getContractId() != null){
                BigDecimal totalTaxMoney = invoiceDetailMap.get(invoice.getId());
                if (result.get(invoice.getContractId()) == null){
                    result.put(invoice.getContractId(), BigDecimalUtil.ZERO);
                }
                result.put(invoice.getContractId(), result.get(invoice.getContractId()).add(totalTaxMoney));
            }
        }
        return result;
    }
}
